package com.hsgene.user.service.impl;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.common.util.validation.FieldChecker;
import com.hsgene.user.condition.GroundServantCondition;
import com.hsgene.user.dto.user.groundservant.GroundServantDto;
import com.hsgene.user.persistence.GroundServantRepository;
import com.hsgene.user.service.GroundServantService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 地服信息impl
 * @ProjectName: hdas-geneshop
 * @Package: com.hsgene.user.service.impl
 * @Author: 江承良
 * @CreateDate: 2018/10/19 9:47
 * @Version: 1.0
 * Copyright: Copyright (c) 2018
 */
@Service
public class GroundServantServiceImpl implements GroundServantService {
    @Resource
    private GroundServantRepository groundServantRepository;

    /***
     * 根据条件查询地服列表
     * @author jcl
     * @date 2018/10/19 11:05
     * @param condition 查询条件
     * @return
     */
    @Override
    public ApiResult groundServants(GroundServantCondition condition) {
        FieldChecker.checkEmpty(condition.getLimitHelper(),"分页条件");
        int count = groundServantRepository.groundServantCount(condition);
        Pagination<GroundServantDto> pagination = Pagination.newInstance(condition.getLimitHelper(), count);
        List<GroundServantDto> groundServantDtos = new ArrayList<>();
        if(count>0){
            groundServantDtos = groundServantRepository.groundServants(condition);
            if(groundServantDtos!=null&&groundServantDtos.size()>0){
                int rowNum = (condition.getLimitHelper().getPage()-1)*condition.getLimitHelper().getSize();
                for (int i =0;i<groundServantDtos.size();i++){
                    GroundServantDto groundServantDto = groundServantDtos.get(i);
                    String regionDesc = "";
                    //拼接区域
                    if(!StringUtils.isEmpty(groundServantDto.getTerritoryName())){
                        regionDesc = regionDesc+groundServantDto.getTerritoryName();
                    }if(!StringUtils.isEmpty(groundServantDto.getTerritoryName())&&!StringUtils.isEmpty(groundServantDto.getAreaName())){
                        regionDesc = regionDesc+"/";
                    } if(!StringUtils.isEmpty(groundServantDto.getAreaName())){
                        regionDesc = regionDesc+groundServantDto.getAreaName();
                    }
                    groundServantDto.setRegionDesc(regionDesc);
                    groundServantDto.setNumber(rowNum+1+i);
                }
            }
        }
        pagination.setResult(groundServantDtos);
        return new ApiResult(200,"success","数据获取成功",pagination);
    }
}
